'''
Created on 21 Oct 2011

@author: rx201
'''
import sys
import shutil
import os.path
from models import App

if sys.platform == "win32":
    APK_ROOT = "y:\\aurasium\\web\\data"
else:
    APK_ROOT = "/home/rx201/aurasium/web/data"
    
UPLOADED_APK_DIR = os.path.join(APK_ROOT, 'data')
PROCESSED_APK_DIR = os.path.join(APK_ROOT, 'output')
LOGFILE_DIR = os.path.join(APK_ROOT, 'log')

for path in [UPLOADED_APK_DIR, PROCESSED_APK_DIR, LOGFILE_DIR]:
    if not os.path.exists(path):
        os.mkdir(path)

def getUploadedAPKPath(apk):
    return os.path.join(UPLOADED_APK_DIR, apk.filehash) + ".apk"

# Processed apk may be different even if original apk is the same,
# as Aurasium itself may be upgraded.
#TODO: Maybe tag i with aurasium code version istead of apk id? 
def getProcessedAPKPath(apk):
    return os.path.join(PROCESSED_APK_DIR, "%d_%s" % (apk.id, apk.filehash)) +".apk"

def storeUploadedAPK(form_file, apk):
    target = getUploadedAPKPath(apk)
    # Do not copy if apk already exists
    if not os.path.exists(target):
        target_file =  open(target, "wb")
        for chunk in form_file.chunks():
            target_file.write(chunk)
        target_file.close()
        
    
    

def getLogFilePath(apk):
    return os.path.join(LOGFILE_DIR, "%d_%s" % (apk.id, apk.filehash)) +".log"

    
